/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.uniscol.editors;

import fr.uniscol.model.Periode;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import org.apache.log4j.Logger;
import org.jdom2.IllegalAddException;

/**
 *
 * @author Christophe
 */
public class PeriodePropertyEditor extends PropertyEditorSupport {
    
    public static Logger log = Logger.getLogger(PeriodePropertyEditor.class);
    
    private static DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        
        Periode periode = new Periode();
        
        text = text.replaceAll("/", "-");
        
        String[] split = text.split("_");
        
        String type = split[0];
        
        try {
            if(type.equalsIgnoreCase("D")){
                // Periode DEBUT
                periode.setDebut(new GregorianCalendar());
                Date debut = dateFormat.parse(split[1]);
                periode.getDebut().setTimeInMillis(debut.getTime());

            }else if(type.equalsIgnoreCase("F")){
                // Période de FIN
                periode.setFin(new GregorianCalendar());
                Date fin = dateFormat.parse(split[1]);
                periode.getFin().setTimeInMillis(fin.getTime());
                
            }else if(type.equalsIgnoreCase("I")){
                // INTERVALLE 
                periode.setDebut(new GregorianCalendar());
                periode.setFin(new GregorianCalendar());
                Date debut = dateFormat.parse(split[1]);
                Date fin = dateFormat.parse(split[2]);
                periode.getDebut().setTimeInMillis(debut.getTime());
                periode.getFin().setTimeInMillis(fin.getTime());
            }

            setValue(periode);
        
        } catch (ParseException ex) {
            log.fatal(ex);
            throw new IllegalArgumentException(ex);
        }
    }

    @Override
    public String getAsText() {
        
        Periode periode = (Periode) getValue();
        
        if(periode == null){
            periode = new Periode();
        }
        
        String text = "";
        if(periode.getDebut() != null && periode.getFin() == null){
            text = "D_" + dateFormat.format(periode.getDebut().getTime());
            
        }else if(periode.getFin() != null && periode.getDebut() == null){
            text = "F_" + dateFormat.format(periode.getFin().getTime());;
            
        }else if(periode.getDebut() != null && periode.getFin() != null){
            text = "I_" + dateFormat.format(periode.getDebut().getTime()) + "_" + dateFormat.format(periode.getFin().getTime());
        }
        
        return text;
    }
}
